package cn.theten52.demo.patterns.design.behavior.state;

/**
 * 自动售货机 => Context角色
 *
 * @author wangjin
 * @date 2020/12/30
 */
public class VendingMachine {
    // 表示当前状态
    private State state = null;
    // 商品数量
    private int count = 0;
    private State chooseGoods = new ChooseGoods(this);
    private State paymentState = new PaymentState(this);
    private State dispenseCommodityState = new DispenseCommodityState(this);
    private State emptyState = new EmptyState(this);

    public VendingMachine(int count) {
        this.count = count;
        this.state = this.chooseGoods;
    }

    // 购买商品
    public void purchase() {
        // 挑选商品
        state.choose();
        // 支付成功
        if (state.payment()) {
            // 分发商品
            state.dispenseCommodity();
        }
    }

    // 获取商品后将商品减一
    public int getCount() {
        return count--;
    }

    // get和set方法 ...

    public State getState() {
        return state;
    }

    public State getChooseGoods() {
        return chooseGoods;
    }

    public State getPaymentState() {
        return paymentState;
    }

    public State getDispenseCommodityState() {
        return dispenseCommodityState;
    }

    public State getEmptyState() {
        return emptyState;
    }

    public void setState(State state) {
        this.state = state;
    }
}
